<?php

namespace App\Http\Controllers\Web\Product;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ProductController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'product'],
        'show' => ['get', 'product/%d'],
        'store' => ['post', 'product'],
        'update' => ['put', 'product/%d'],
        'comments'=>['get','product/comments/%d'],
        'delete' => ['delete', ''],
        'attr_index'=> ['get', 'attr'],
        'cate_index'=> ['get', 'cate'],
        'cate'=> ['get', 'cate/%d'],
        'brand_index'=> ['get', 'brand/%d'],
        'brands'=> ['get', 'brand'],

        'collection_check' => ['get',  'buyer/collection/checkCollection/%d/%d'],
        'collection_bat_check'=>['post','buyer/collection/checkMultiCollection'],
        'collection_add'  => ['post', 'buyer/collection'],
        'shop'=> ['get', 'shop'],
        
        'skus'=>['get','skus'],
        'qualified'=>['get','qualified/products'], // 获取商品列表
        'showShop'     => ['get', 'qualified/qualifiedProduct'],       // 按条件 获取含资质服务商品的店铺列表
        'shopImCheck' => ['post',  'im/shopImCheck/%d'], // 检测店铺用户是否有 IM 账户
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        
    }
    
    public function create(){
    }
    

    public function show($product_id,$sku_id=0){

        if(Redis::exists('product_show_data_'.$product_id)){
            $ret= \GuzzleHttp\json_decode(Redis::get('product_show_data_'.$product_id),true);
        }else{
            $ret  = http_web($this->api_url['show'], [$product_id])->send();
            Redis::setex('product_show_data_'.$product_id,3600*3,json_encode($ret));
            $ret= \GuzzleHttp\json_decode(Redis::get('product_show_data_'.$product_id),true);
        }

        if(isset($ret['code']) && $ret['code'] === 0){
            $data['product'] = $ret['data'];
            $attrs = [];
            $attrs_value = [];
            if(isset($ret['data']['skus'][0])){
                $data['sku'] = $ret['data']['skus'][0];
            }
         
            if($data['product']['status'] != 1   && $data['product']['user_id'] != session("user_id")){
                echo "404";exit();
            }
 
            if(!$ret['data']['skus']){
                echo "404";exit();
            }
            foreach($ret['data']['skus'] as $sku){
                if($sku['id'] == $sku_id){
                    $data['sku'] = $sku;
                }
                
                $attributes= explode(";",$sku['attributes']);
                if(sizeof($attributes) >0){
                    foreach ($attributes as $aa){
                        $a1 = explode(",",$aa);
                        if(sizeof($a1) == 2){
                            $tmp = explode(":",$a1[0]);
                            if(sizeof($tmp) > 1 && $tmp[0]){
                                $attrs[$tmp[0]] =$tmp[1] ;
                            }
                            
                            
                            $tmp1 = explode(":",$a1[1]);
                            if(sizeof($tmp1) > 1 && $tmp1[0]){
                                $attrs_value[$tmp[0]][$tmp1[0]] =$tmp1[1] ;
                            }
                            
                        }
                    }
                }
            }
 
            $search_ids = implode(",",$attrs);
            $data['attrs'] =$attrs; 
            $data['attrs_value'] =$attrs_value;

            $cates = [];
            //
            if(Redis::exists("categorys")){
                $onecate= Redis::hget("categorys",$data['product']['first_cat']);
                if($onecate){
                    $cates[]  = json_decode($onecate,TRUE);
                }
                $onecate= Redis::hget("categorys",$data['product']['second_cat']);
                if($onecate){
                    $cates[]  = json_decode($onecate,TRUE);
                }
                $onecate= Redis::hget("categorys",$data['product']['third_cat']);
                if($onecate){
                    $cates[]  = json_decode($onecate,TRUE);
                }
                
                
            }
            //
            if(!$cates){
                $params = [
                    'search' => search_params(['id'=>$data['product']['first_cat'].",".$data['product']['second_cat'].",".$data['product']['third_cat']]),
                ];

                if(Redis::exists('product_cates_data')){
                    $dbcates= \GuzzleHttp\json_decode(Redis::get('product_cates_data'),true);
                }else{
                    $dbcates =  http_web($this->api_url['cate_index'])->send($params);
                    Redis::setex('product_cates_data',3600*3,json_encode($dbcates));
                    $dbcates= \GuzzleHttp\json_decode(Redis::get('product_cates_data'),true);
                }
                if(isset($dbcates['code']) && $dbcates['code']===0){
                    $cates = $dbcates['data']['list'];
                }
            }
            
        
          

            $data['c1']  =$data['c2'] = $data['c3']=""; 
            $data['c1_id']  =$data['c2_id'] = $data['c3_id']=""; 
            if($cates){
                foreach($cates as $cate){
                    if($cate['id'] == $data['product']['first_cat']){
                        $data['c1'] = $cate['name'];
                        $data['c1_id'] = $cate['id'];
                    }
                    if($cate['id'] == $data['product']['second_cat']){
                        $data['c2'] = $cate['name'];
                        $data['c2_id'] = $cate['id'];
                    }
                    if($cate['id'] == $data['product']['third_cat']){
                        $data['c3'] = $cate['name'];
                        $data['c3_id'] = $cate['id'];
                    }
                }
            }
             
            $data['skus']=json_encode($data['product']['skus']);
            unset($data['product']['skus']);

            if(Redis::exists('product_brand_data_'.$data['product']['brand_id'])){
                $brand_ret= \GuzzleHttp\json_decode(Redis::get('product_brand_data_'.$data['product']['brand_id']),true);
            }else{
                $brand_ret =  http_web($this->api_url['brand_index'],[$data['product']['brand_id']])->auto(false)->send();
                Redis::setex('product_brand_data_'.$data['product']['brand_id'],3600*3,json_encode($brand_ret));
                $brand_ret= \GuzzleHttp\json_decode(Redis::get('product_brand_data_'.$data['product']['brand_id']),true);
            }

            if(isset($brand_ret['data']['data']['name'])){
                $data['brnad'] = $brand_ret['data']['data']['name'];
            }else{
                $data['brnad'] = "";
            }
            $params = [
                'search' => search_params(['is_for_sale'=>1, 'user_id'=>$data['product']['user_id'], 'third_cat'=>$data['product']['third_cat']]),
                'searchJoin'=>'and',
                'with'=>'skus',
            ];


            $related_products =  http_web($this->api_url['index'])->send($params);


            if(isset($related_products['code']) && $related_products['code'] ===0 && isset($related_products['data']['list'])){
                $data['related_product'] = $related_products['data']['list'];
            }

            $params = [
                'search' => search_params(['sp_id'=>$data['product']['shop_id']]),
                'searchJoin'=>'and',
            ];
            if(Redis::exists('product_shop_data_'.$data['product']['shop_id'])){
                $shop= \GuzzleHttp\json_decode(Redis::get('product_shop_data_'.$data['product']['shop_id']),true);
            }else{
                $shop =  http_web($this->api_url['shop'])->send($params);
                Redis::setex('product_shop_data_'.$data['product']['shop_id'],3600*3,json_encode($shop));
                $shop= \GuzzleHttp\json_decode(Redis::get('product_shop_data_'.$data['product']['shop_id']),true);
            }

            $data['shop'] = [];
            if(  isset($shop['data']['list']) && sizeof($shop['data']['list']) > 0){
                $data['shop'] = $shop['data']['list'][0];
            }
            try {
                $check = http_web($this->api_url['collection_check'], [$sku_id,$data['product']['shop_id']])->send();
                $data['check'] = $check['data'];
                // 检测用户商户是否有 IM 账号
                $shop_uid = $data['shop']['user']['id'];
                $shopImCheck = http_web($this->api_url['shopImCheck'], [$shop_uid])->send();

                $data['shopImCheck'] = isset($shopImCheck['data'])?$shopImCheck['data']:false;
            }catch (\Exception $e){
                //dd($e->getMessage());
            }
            $data['search_params']['shop_id'] = $data['product']['shop_id'];
            return view('web.product.product_detail', $data);
        } 
        
    }
    
    public function list($cate_id,Request $request){
        $brand_id = $this->request->input("brand_id",0);
        $order = $this->request->input("order",0);
//        dd($order);
        $onecate =  http_web($this->api_url['cate'],[$cate_id])->send();

        // 找到该分类 id 的 cate_type
        $qualified = false;
        if ($onecate['data']['data']['cate_type']==2) $qualified = true;
     
        //参数部分
        $search_params = $this->request->all(['categories_data','attrs_data','order','brand_id','sale_price']);
        $search_params_attr = $this->request->all(['categories_data','attrs_data','order','brand_id','sale_price','attrs_attributes','attrs_data2']);
//        dd($search_params_attr);
        $search_params['product_status'] = 1;
        $bp = [];
        if($cate_id){
            $bp =['copy_category'=>$cate_id];
            if(isset($onecate['data']['data']['id'])){
                if($onecate['data']['data']['pid'] != $onecate['data']['data']['first_id']){
                    $search_params['third_cat']=$cate_id;
                }else if($onecate['data']['data']['pid']==0){
                    $search_params['first_cat']=$cate_id;
                }else{
                    $search_params['second_cat']=$cate_id;
                }
                
            }
           
        }
       
        if( isset($search_params['categories_data']['id'])){
            $search_params['third_cat']=$search_params['categories_data']['id'];
        }
     
        if( $brand_id){
            $search_params['brand_id']=$brand_id;
        }
        
        if( isset($search_params['attrs_data'])){
            $search_params['attrs_attributes']=$search_params['attrs_data'];
        }
        
        if( isset($search_params['attrs_data'])){
            $search_params['attributes']=$search_params['attrs_data'];
        }
        if( isset($search_params_attr['attrs_data2'])){
            $search_params['attrs_data2']=$search_params_attr['attrs_data2'];
        }
        $search_params['is_for_sale']=1;
        $s_attrs = [];
        $data['pattrs'] = [];
        $page = $this->request->get('page', 1);
        // 区分资质分类 和 撮合普通分类 页面
        if ($qualified == false) { // 原商品查询 begin

            $params = [
                'search' => search_params($search_params),
                'searchJoin' => 'and',
                'page' => $page,
                'orderBy' => 'id',
                'sortedBy' => 'desc',
                'with' => 'skus',
            ];
            if ($order == 4) {
                $params['orderBy'] = "sale_price";
            } else {
                $params['orderBy'] = "id";
            }
            //请求部分
            $ret = http_web($this->api_url['skus'])->send($params);
            //结果处理部分
            $data = paginate($this->request, $ret['data']);
         
            if (isset($ret['data']['list'])) {
                $sku_ids = "";
                foreach ($ret['data']['list'] as $pl) {
                    $sku_ids .= "," . $pl['id'];
                    
                    if(isset($pl['attributes']) && strlen($pl['attributes']) > 4  ){
                        
                        $atmp = explode(";",$pl['attributes']);
                        
                        if(is_array($atmp)){
                            foreach($atmp as $at){
                                $t = explode(",",$at);
                                if(isset($t[0]) && $t[0]){
                                    $as = explode(":",$t[0]);
                                    if(isset($t[1]) && $t[1]){
                                        $sv = explode(":",$t[1]);
                                        if(sizeof($sv) == 2 && $sv[1] !="-"){
                                            $s_attrs[$as[0]]['val'][$sv[0]] = $sv[1];
                                        }
                                        if($as[1]!="-"){
                                            $s_attrs[$as[0]][$as[0]] = $as[1];
                                        }
                                        
                                    }
                                }
                                
                                
                            }
                            
                        }
                        
                    }
                }

                $check = http_web($this->api_url['collection_bat_check'])->auto(false)->send(['sku_ids' => $sku_ids]);

                $data['check'] = $check['data'];
            }


        }// 原商品查询 end
        
        $data['pattrs'] = $s_attrs;
     
        // 分类信息整理 begin
        //回填搜索框值
        $data['search_params'] = $search_params;
        $brand_params = [
            'search' => search_params($bp),
            'searchJoin' => 'and',
            'type'=>'all',
        ];
        $brand_ret =  http_web($this->api_url['brands'])->auto(false)->send($brand_params);

        $data['brands'] = [];
        if(isset($brand_ret['data']['list'])){
            if($cate_id){
                foreach($brand_ret['data']['list'] as $brands){
                    if( in_array($cate_id , explode(",",$brands['copy_category']))){
                        $data['brands'][] = $brands;
                    }
                }
            }else{
                $data['brands'] = $brand_ret['data']['list'];
            }

        }


        $data['cates'] = [];
        $data['nav_cate']=['name'=>'','id'=>''];

        $data['cate_name'] = "";
        $data['attrs'] = [];

        if(isset($onecate['data']['data']['id'])){
            $data['cate_name']  =$onecate['data']['data']['name'];
            $cparams = [
                'search' => search_params(['pid'=>$onecate['data']['data']['id']]),
                'type'=>'all'
            ];
            $cates =  http_web($this->api_url['cate_index'])->send($cparams);
            if(isset($cates['data']['list'])){
                $data['cates'] = $cates['data']['list'];
            }


            $parent_cate =  http_web($this->api_url['cate'],[$onecate['data']['data']['pid']])->auto(false)->send();
            if(isset($parent_cate['data']['data']['id'])){
                $data['nav_cate']['id']=$parent_cate['data']['data']['id'];
                $data['nav_cate']['name']=$parent_cate['data']['data']['name'];

            }

            if( $onecate['data']['data']['attrs']) {
                $attrs = json_decode($onecate['data']['data']['attrs'],true);
                $data['attrs'] = $attrs;
            }
        }

        if(isset($search_params['third_cat'])){
            $last_cate =  http_web($this->api_url['cate'],[$search_params['third_cat']])->auto(false)->send();

            if(isset($last_cate['data']['data']['id'])){
                $attrs = json_decode($last_cate['data']['data']['attrs'],true);
                $data['attrs'] = $attrs;
            }

        }
        if($data['attrs']){
            sort($data['attrs']);
        }

        $data['cate_id'] = $cate_id;
        // 分类信息整理 end
        // 区分资质分类 和 撮合普通分类 页面
        if ($qualified == false){
            return view('web.product.product_allist', $data);
        }else{
            // 根据商品列表显示店铺列表
            $QParams = [
                'second_cat' => isset($search_params['second_cat'])?$search_params['second_cat']:'',
                'third_cat'  => isset($search_params['third_cat'])?$search_params['third_cat']:'',
                'is_for_sale'=> isset($search_params['is_for_sale'])?$search_params['is_for_sale']:'',
                'attrs_attributes'=> isset($search_params['attrs_attributes'])?$search_params['attrs_attributes']:'',
            ];
            $selectParams = [
                'search' => search_params($QParams),
                'searchJoin' => 'and',
                'page' => $page,
                'with'=>'attrs'
            ];
//            152:盖章费(元),1036:6;153:实施费%,1035:4
            $Qproducts = http_web($this->api_url['qualified'])->send($selectParams);
            // 获取店铺列表
            $shop_ids = implode(',', array_unique(array_filter(array_column($Qproducts['data'], 'shop_id'))));
            $price=$this->request->all(['price']);
            if ($shop_ids != ""){
                // 查询店铺列表条件
                $search_shop = [ // 此数组已重置
                    'price'=>$price['price'],
                    'shop_id' => $shop_ids,
                    'third_cat'  => isset($search_params['third_cat'])?$search_params['third_cat']:'',
                    'attributes'=> isset($search_params['attrs_attributes'])?$search_params['attrs_attributes']:'',
                ];
                $sParams = [
                    'search' => search_params($search_shop),
                    'page' => $page,
                    'searchJoin' => 'and',
                    'orderBy'=>'id',
                    'sortedBy'=>'desc',
                    'attributes2'=> !empty($search_params_attr['attrs_data2'])?$search_params_attr['attrs_data2']:'',
                ];
                // 请求部分 - 根据 shop_id 查询有此服务的店铺列表
            }else{
                $search_shop = [ // 此数组已重置
                    'price'=>$price['price'],
                    'third_cat'  => isset($search_params['third_cat'])?$search_params['third_cat']:'',
                    'attributes'=> isset($search_params['attrs_attributes'])?$search_params['attrs_attributes']:'',
                ];
                $sParams = [
                    'search' => search_params($search_shop),
                    'searchJoin' => 'and',
                    'page' => $page,
                    'orderBy'=>'id',
                    'sortedBy'=>'desc',
                    'attributes2'=> !empty($search_params_attr['attrs_data2'])?$search_params_attr['attrs_data2']:'',
                ];
            }
            if ($order == 4) {
                $sParams['orderBy'] = "price";
            } else {
                $sParams['orderBy'] = "id";
            }
            // 根据 product_id 查询出 shop_id 提取店铺列表
            $shop = http_web($this->api_url['showShop'])->send($sParams);
            $shop = paginate($this->request, $shop['data']);

            if(!empty($shop['list'])){
                foreach($shop['list'] as $k=>$v){
                    $arr=explode(';',$v['attributes']);
                    $attr_arr=[];
                    foreach($arr as $key=>$val){
                        $arr2=explode(',',$val);
                        $arr[$key]=$arr2;
                        foreach($arr2 as $key2=>$val2){
                            $arr3=explode(':',$val2);
                            $arr[$key][$key2]=$arr3;
                            foreach($arr3 as $k3=>$v4){
                                if($k3==1){
                                    $attr_arr[$key][]=$v4;
                                }
                            }
                        }
                    }
                    $level='';
                    foreach($attr_arr as $k1=>$v1){
                        if($v1[0]=='资质类别'){
                            $level=$v1[1];
                        }
                    }
                    $shop['list'][$k]['level']=$level;
                    $shop['list'][$k]['attributes']=$arr;
                    $shop['list'][$k]['attr_arr']=$attr_arr;

                }
            }

            $data['list'] = $shop['list'];
            $data['page'] = $shop['page'];

            // 属性id
            $attrss = explode(',',$search_params_attr['attrs_data']);

            $arri_arr=[];
            foreach ($attrss as $k=> &$v){
                $v = explode(':',$v);
                if(isset($v[1])){
                    $arri_arr[]=$v[1];
                }
            }

            $attrss2 = explode(',', $search_params_attr['attrs_data2']);
            foreach ($attrss2 as $k=> &$v){
                $v = explode(':',$v);
                if(isset($v[1])) {
                    $arri_arr[]=$v[1];
                }
            }

            $data['search_params']['arri_arr'] = $arri_arr;
            $data['search_params']['attributes'] = $attrss;
            $data['search_params']['attrs_data2'] = $search_params_attr['attrs_data2'];
            return view('web.find.aptitude', $data);
        }
    }
    
    public function search(Request $request){
        $brand_id = $this->request->input("brand_id",0);
        $order = $this->request->input("order",0);
        
        //参数部分
        $search_params = $this->request->all(['product_name', 'categories_data','attrs_data','order','brand_id','sale_price']);
        if (isset($search_params['product_name']) && strlen($search_params['product_name'])>26) {
            $search_params['product_name'] = mb_substr($search_params['product_name'], 0,26);
            //$search_params['character'] = true;
        }
        $search_params['product_name'] = str_replace([';','eval'],' ',$search_params['product_name']);
            $search_params['product_status'] = 1;
        $bp = [];
       
        
        if( isset($search_params['categories_data']['id'])){
            $search_params['third_cat']=$search_params['categories_data']['id'];
        }
        
        if( $brand_id){
            $search_params['brand_id']=$brand_id;
        }
        
        if( isset($search_params['attrs_data'])){
            $search_params['attributes']=$search_params['attrs_data'];
        }
        $search_params['is_for_sale']=1;
        $page = $this->request->get('page', 1);
        
        
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'orderBy'=>'id',
            'sortedBy'=>'desc',
            'with' => 'skus',
        ];
        
        if($order == 4){
            $params['orderBy'] = "sale_price";
        }else{
            $params['orderBy'] = "id";
        }
        $data['brands'] = [];
        $data['cates'] = [];
        //请求部分
        $ret = http_web($this->api_url['skus'])->send($params);
        $s_attrs = [];
        $s_attr_val=[];
   
        //结果处理部分
        $data = paginate($this->request, $ret['data']);
        $cate_ids=""; 
        $brand_ids = "";
        $data['pattrs'] = [];
        if(isset($ret['data']['list'])){
            foreach($ret['data']['list'] as $d){
                if($d['product']['third_cat']){
                    $cate_ids .=",".$d['product']['third_cat'];
                }
                if($d['product']['brand_id']){
                    $brand_ids .=",".$d['product']['brand_id'];
                }
                
                if(isset($d['attributes']) && strlen($d['attributes']) > 4  ){
                   
                    $atmp = explode(";",$d['attributes']);
                    
                    if(is_array($atmp)){
                        foreach($atmp as $at){
                            $t = explode(",",$at);
                            if(isset($t[0]) && $t[0]){
                                $as = explode(":",$t[0]);
                                if(isset($t[1]) && $t[1]){
                                    $sv = explode(":",$t[1]);
                                    if(sizeof($sv) == 2 && $sv[1] !="-"){
                                        $s_attrs[$as[0]]['val'][$sv[0]] = $sv[1];
                                    }
                                    if($as[1]!="-"){
                                        $s_attrs[$as[0]][$as[0]] = $as[1];
                                    }
                                    
                                }
                            }
                            
                           
                        }
                        
                    }
                    
                }
               
            }
            $data['pattrs'] = $s_attrs;
       
            if($cate_ids){
                $cparams = [
                    'search' => search_params(['id'=>$cate_ids]),
                    'searchJoin' => 'and',
                    'type'=>'all',
                ];
                $cates =  http_web($this->api_url['cate_index'])->send($cparams);
                if(isset($cates['data']['list'])){
                    $data['cates'] = $cates['data']['list'];
                }
            }
            
            if($brand_ids){
                $bparams = [
                    'search' => search_params(['id'=>$brand_ids]),
                    'searchJoin' => 'and',
                    //'type'=>'all',
                ];
                $brands =  http_web($this->api_url['brands'])->send($bparams);
                if(isset($brands['data']['list'])){
                    $data['brands'] = $brands['data']['list'];
                }
            }
        }
      
     
        if(isset($ret['data']['list']) ){
            $sku_ids = "";
            foreach($ret['data']['list'] as $pl){
                $sku_ids .=",".$pl['id'];
            }
            
            
            
            $check = http_web($this->api_url['collection_bat_check'])->auto(false)->send(['sku_ids'=>$sku_ids]);
            
            $data['check'] = $check['data'];
        }
        
        
        
        //回填搜索框值
        $data['search_params'] = $search_params;
        
        $brand_params = [
            'search' => search_params($bp),
            'searchJoin' => 'and',
            'type'=>'all',
        ];
        $brand_ret =  http_web($this->api_url['brands'])->auto(false)->send($brand_params);
        
        
        
  
        $data['nav_cate']=['name'=>'','id'=>''];
        
        $data['cate_name'] = "";
        $data['attrs'] = [];
        
        if(isset($onecate['data']['data']['id'])){
            $data['cate_name']  =$onecate['data']['data']['name'];
            $cparams = [
                'search' => search_params(['pid'=>$onecate['data']['data']['id']]),
            ];
            $cates =  http_web($this->api_url['cate_index'])->send($cparams);
            if(isset($cates['data']['list'])){
                $data['cates'] = $cates['data']['list'];
            }
            
            
            $parent_cate =  http_web($this->api_url['cate'],[$onecate['data']['data']['pid']])->auto(false)->send();
            if(isset($parent_cate['data']['data']['id'])){
                $data['nav_cate']['id']=$parent_cate['data']['data']['id'];
                $data['nav_cate']['name']=$parent_cate['data']['data']['name'];
                
            }
            
            if( $onecate['data']['data']['attrs']) {
                $attrs = json_decode($onecate['data']['data']['attrs'],true);
                $data['attrs'] = $attrs;
            }
        }
        
        if(isset($search_params['third_cat'])){
            $last_cate =  http_web($this->api_url['cate'],[$search_params['third_cat']])->auto(false)->send();
            
            if(isset($last_cate['data']['data']['id'])){
                $attrs = json_decode($last_cate['data']['data']['attrs'],true);
                $data['attrs'] = $attrs;
            }
            
        }
        if($data['attrs']){
            sort($data['attrs']);
        }
        
        
        return view('web.product.product_search', $data);
    }
    
    public function getComments($product_id, Request $request)
    {
        $params = [
            'with'=>'user;productSku;orderProduct',
            'searchJoin'=>'and',
            'page' => $request->input('page'),
        ];
        
        $type =  $request->input('type');
        if($type > 0){
            $params['search'] =search_params(['overall'=>$type])  ; 
        }
     
        $comments_ret =  http_web($this->api_url['comments'],[$product_id])->send($params);
      
        if(isset($comments_ret['code']) && $comments_ret['code'] == 0){
            if(isset($comments_ret['data']['meta']['pagination'])){
                $ret['page'] = $comments_ret['data']['meta']['pagination'];
                $ret['page']['page_total'] = $ret['page']['total_pages'];
            }
            if(isset($comments_ret['data']['data'])){
                $ret['data'] = $comments_ret['data']['data'];
            }
            
            $data = paginate($this->request, $ret);
            return view('web.product.comments', $data);
        } 
          
    }

    public function update($id){

    }
    /**
     * 商品详情页，商品收藏，店铺收藏
     * */
    public function addCollection(Request $request){
        try{
            $this->validate($request, [
                'shop_id' => 'required|numeric',// product 商品收藏，shop 店铺收藏
                'type' => 'required',
                'product_id' => 'required_with_all:type,shop|integer',
//                'sku_id' => 'required_with_all|product_id',
            ], [
                'shop_id' => 'shop_id参数必填|shop_id类型传入错误',// product 商品收藏，shop 店铺收藏
                'type' => '类型参数必填',
                'product_id' => '商品 id 不可为空|商品id必须是数字',
//                'sku_id' => 'sku_id 不可为空',
            ]);

            $params = $request->all();
            $params['type'] = $params['type']=='shop'?2:1;
            $params['is_invalid'] = 1;
            if ($params['type']==2&&isset($params['product_id'])){
                unset($params['product_id']);
            }
            $ret  = http_web($this->api_url['collection_add'])->send($params);

        }catch (\Exception $e){
            return response()->json(['code'=>1,'message'=>$e->getMessage()]);
        }
        return response()->json($ret);
    }

}